<?php
/**
 * Student Model
 *
 * Utilizes the Data Mapper pattern to persist data. Represents a single
 * student entry
 *
 * @version $Id: Student.php 135 2010-07-13 10:04:00Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Student_Model_StudentMapper
 * @package student
 * @subpackage Model
 */
class Student_Model_Student extends Student_Model_Doctrine_BaseStudent
{
    const IN_REGISTER = 0;
    /**
     *  get students from a class
     * @param $year_semester
     * @param $class_id
     * @return object
     */
    public function getClassStudent($year,$semester,$grade,$classSn, $studyCondition = null) //,$studyCondition=self::IN_REGISTER)
    {

        $q = Doctrine_Query::create()
        ->select('a.name, a.sex, a.sn ,a.study_condition ,b.student_id AS student_id, b.number AS number')
        ->from('Student_Model_Student a ')
        ->leftJoin('a.Student_Model_StudentSemester b ')
        ->where ('b.year=? AND b.semester=? AND b.grade=? AND b.class_sn=?',
        array($year,$semester,$grade,$classSn));

        if ($studyCondition !== null)
        $q->andWhere('study_condition=?' , $studyCondition);

        $q->orderBy('b.number');

        $res = $q->fetchArray();
        $arr = array();
        foreach($res as $row) {
            $arr[$row['sn']] = $row;
        }
        return $arr;
    }

    /**
     * 取出學生就學狀態陣列
     * @return array
     */
    public static function getStudentConditionArray()
    {
        $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = dirname(__FILE__)."/../languages/$locale/data.ini";
        $ini = new Zend_Config_Ini($path);
        $arrayData = $ini->get('array_data')->toArray();
        return $arrayData['study_cond'];
    }

    /**
     *  拆解住址
     * @param string $addr
     * @param integer $mode
     * @return array|string
     */
    function splitAddress($addr,$mode=0) {
        $addr = trim($addr) ;
        //縣市
        $temp_str = $this->_splitStr($addr,"縣",1);
        if ($temp_str[0] =="")
        $temp_str = $this->_splitStr($addr,"市",1);
        $res[]=$temp_str[0] ;
        $addr=$temp_str[1];

        //鄉鎮
        $temp_str = $this->_splitStr($addr,"鄉",1);
        if ($temp_str[0] =="")
        $temp_str = $this->_splitStr($addr,"鎮",1);

        if ($temp_str[0] =="")
        $temp_str = $this->_splitStr($addr,"市",1);

        if ($temp_str[0] =="")
        $temp_str = $this->_splitStr($addr,"區",1);

        $res[]=$temp_str[0] ;
        $addr=$temp_str[1];

        //mode=2, 只取縣市鄉鎮
        if ($mode==2) return $res;

        //村里
        $temp_str = $this->_splitStr($addr,"村",1);
        if ($temp_str[0] =="")
        $temp_str = $this->_splitStr($addr,"里",1);

        $res[]=$temp_str[0] ;
        $addr=$temp_str[1];

        //鄰
        $temp_str = $this->_splitStr($addr,"鄰",$mode);
        $res[]=$temp_str[0] ;
        $addr=$temp_str[1];

        //路
        $temp_str = $this->_splitStr($addr,"路",1);
        if ($temp_str[0] =="")
        $temp_str = $this->_splitStr($addr,"街",1);

        $res[] = $temp_str[0];
        $addr=$temp_str[1];

        //段
        $temp_str = $this->_splitStr($addr,"段",$mode);
        $res[]=$temp_str[0] ;
        $addr=$temp_str[1];

        //巷
        $temp_str = $this->_splitStr($addr,"巷",$mode);
        $res[]=$temp_str[0] ;
        $addr=$temp_str[1];

        //弄
        $temp_str = $this->_splitStr($addr,"弄",$mode);
        $res[]=$temp_str[0] ;
        $addr=$temp_str[1];

        //號
        $temp_str = $this->_splitStr($addr,"號",$mode);
        if (strpos($temp_str[0], '之')) {
            $temp_arr2 = explode("之", $temp_str[0]);
            $res[] = $temp_arr2[0] ;
            $res[] = $temp_arr2[1];
        }
        else {
            $res[] = $temp_str[0] ;
            $res[] = '';
        }

        $addr=$temp_str[1];


        //樓
        $temp_str = $this->_splitStr($addr,"樓",$mode);
        $res[]=$temp_str[0] ;
        $addr=$temp_str[1];

        //樓之
        if ($addr != "") {
            if ($mode)
            $temp_str = $addr;
            else
            $temp_str = substr(chop($addr),2);
        } else
        $temp_str ="";

        $res[]=$temp_str ;
        return $res;
    }

    protected  function _splitStr($addr,$str,$last=0) {
        $temp = explode ($str, $addr);
        if (count($temp)<2 ){
            $t[0]="";
            $t[1]=$addr;
        }else{
            $t[0]=(!empty($last))?$temp[0].$str:$temp[0];
            $t[1]=$temp[1];
        }
        return $t;
    }
}
